package ferr

import "gitee.com/xluomo945/fm/helper"

type FError interface {
	Code() int
	String() string
	Origin() error
}

type eError struct {
	code   int
	msg    string
	origin error
}

func (e *eError) Error() string {
	if helper.StringEmpty(e.msg) {
		if e.origin == nil {
			return "unknown error"
		}
		return e.origin.Error()
	}
	return e.msg
}

func (e *eError) Code() int {
	return e.code
}

func (e *eError) Origin() error {
	return e.origin
}

func (e *eError) String() string {
	return e.Error()
}

func New(code int, msg string) error {
	return &eError{code: code, msg: msg}
}

func NewByOrigin(code int, msg string, origin error) error {
	return &eError{code: code, msg: msg, origin: origin}
}
